<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>形式参数默认值其它细节</h1>
    <h2>函数的length属性（了解）</h2>
    <ul>
        <li>用于获取没有设置默认值的形式参数个数。</li>
        <li>细节：只能统计到从左向右，第一个设置了默认值的参数就结束了。</li>
        <li>细节：后面学习的rest参数，也不会被统计到。</li>
    </ul>
    <h2>允许使用变量作为形式参数默认值</h2>
    <p>注意作用域问题</p>
    <ul>
        <li>函数外面的全局变量，可以作为形式参数默认值。</li>
        <li>函数前面的形式参数，可以作为后面形式参数的默认值。</li>
    </ul>
</body>
<script>
    function sum(x,y) {
        console.log(x + y);
    }

    console.log(sum.length); // 2

    function sum2(x,y = 0) {
        console.log(x + y);
    }

    console.log("sum2.length = ",sum2.length); // 1

    function sum3(x = 0,y) {
        console.log(x + y);
    }

    console.log("sum3.length = ",sum3.length); // 0


    function sum4(x,y = 0,z) {
        console.log(x + y + z);
    }
    console.log("sum4.length = ",sum4.length); // 1

    // rest参数z，也不会被length统计到
    function sum5(x,y,...z) {

    }
    console.log("sum5.length = ",sum5.length); // 2

    let num = 1;

    function chengfa(a = num,b = num) {
        console.log(a * b);
    }

    chengfa(); // 1

    function chengfa2(a = num,b = a) {
        console.log(a * b);
    }
    chengfa2(); // 1
    chengfa2(2); // 4

</script>
</html>